#include <iostream>
#include "socketuh.h"
#include <time.h>

using namespace std;

int main(int argcm, char **argv)
{
	int listenfd;
	int connfd;
	struct sockaddr_in servaddr;
	char buff[MAXLINE];
	time_t ticks;
	
	listenfd = Socket(AF_INET, SOCK_STREAM, 0);
	
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);// WTF???
	
	cout << INADDR_LOOPBACK << endl << htonl(INADDR_LOOPBACK) << endl;
	servaddr.sin_port = htons(1321);
	
	if(bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0)
	{
		
		printf("bind error\n");
		return -1;
	}
	
	if(listen(listenfd, 8) < 0)
	{
		printf("listen error\n");
		return -1;
	}
	
	while(true)
	{
		cout << "Waiting for connection...\n";
		connfd = accept(listenfd, (sockaddr *) NULL, NULL);
		
		ticks = time(NULL);
		snprintf(buff, sizeof(buff), "%.24s\er\en", ctime(&ticks));
		write(connfd, buff, strlen(buff));
		
		close(connfd);
		cout << "Data sended.\n";
		
	}
}

